package org.jeecg.modules.basic.entry.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.constant.VxeSocketConst;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.basic.electronic.service.ILwgyInoutElectronicService;
import org.jeecg.modules.basic.entry.entity.FreeChargeBean;
import org.jeecg.modules.basic.entry.entity.LwgyInoutRecord;
import org.jeecg.modules.basic.entry.entity.RechargeBean;
import org.jeecg.modules.basic.entry.mapper.LwgyInoutRecordMapper;
import org.jeecg.modules.basic.entry.service.ILwgyElectricControlService;
import org.jeecg.modules.basic.entry.util.WsdlUtil;

import org.jeecg.modules.basic.entry.util.wsCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.jeecg.modules.basic.entry.util.DkUtils.DkURL;
import static org.jeecg.modules.basic.entry.util.DkUtils.DkCdURL;
/**
 * @Description: TODO
 * @author: sn
 * @date: 2021年03月09日 8:51
 */
@Service
public class ILwgyElectricControlServiceImpl extends ServiceImpl<LwgyInoutRecordMapper, LwgyInoutRecord> implements ILwgyElectricControlService {

    private List<String[]> strings = new CopyOnWriteArrayList<>();

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    //public final static  String url = "http://192.168.0.110:8012/LwDkService.asmx";

    //public final static  String url = "http://3r36196c88.qicp.vip/LwDkService.asmx";

    @Autowired
    private ILwgyInoutElectronicService iILwgyInoutElectronicService;

    @Override
    public String useSBKGData(String xq,String fj,String lx,String xl) {
        String result = "";
        Map params = new HashMap<String, String>();
        params.clear();
        try {
            params.put("inMm", "60C85273A0D8834B798C5FA766D76B866B609AA755C3584F797F798C7FF8785D");
            params.put("inXq", xq);
            params.put("inFj", fj);
            params.put("inLx", lx);//开关类型，1开2关
            params.put("inXl", xl);//线路 1照明 2空调
            if(lx.equals("2")){
                pushCloseRoomInfo(xq,fj);//推送关断信息到大屏
            }
            if(DkURL.length()<5){
                DkURL=iILwgyInoutElectronicService.getDkinterface();
            }
            System.out.println(">>>>>>>>>>>>>>>>>2" +DkURL);
            result = WsdlUtil.callService(DkURL, "SetSBKGData",  params, result);
            System.out.println(">>>>>>>>>>>>>>>>>" + result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
        return result;
    }

    @Override
    public void pushCloseRoomInfo(String xq, String fj) {
        JSONObject obj = new JSONObject();//创建json对象
        obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);//指定消息类型为，用户类型
        strings.add(new String[]{xq,fj, "<span class='colorGrass'>"+ LocalTime.now()+"</span>"});//关断的房间
        Collections.reverse(strings);//设置倒序
        obj.put(WebsocketConst.MSG_TXT, strings);
        //发送给前端，指定用户为root，指定页面为大屏{bigScreen}
        sysBaseAPI.sendMessageTo("31c25de75c304466ab5d460ce4fc908d","bigScreen",obj.toJSONString());
        if(strings.size()>=50){//当集合中的数量大于50清掉
            strings.clear();
        }
    }


    @Override
    public RechargeBean showDlZTArray(String xq,String fj) {
        RechargeBean rechargeBean =new RechargeBean();
        String result = "";
        Map params = new HashMap<String, String>();
        params.clear();
        try {
            params.put("inMm", "60C85273A0D8834B798C5FA766D76B866B609AA755C3584F797F798C7FF8785D");
            params.put("inXq", xq);
            params.put("inFj", fj);

            if(DkURL.length()<5){
                DkURL=iILwgyInoutElectronicService.getDkinterface();
            }
            System.out.println(">>>>>>>>>>>>>>>>>2" +DkURL);
            result = WsdlUtil.callService(DkURL, "TraceDlZTArray",  params, result);
            System.out.println(">>>>>>>>>>>>>>>>>" + result);


            String bb = String.valueOf(result);
            Pattern p = Pattern.compile("<string>(.*?)</string>");
            Matcher m = p.matcher(bb);
            m.find();
            if (m.group(1).equals("-1")||m.group(1).equals("ErrSb")||m.group(1).equals("-2")||m.group(1).equals("-3")) {
                rechargeBean.setMsg("1");
                return rechargeBean;
            }
            rechargeBean.setFjh(m.group(1));
            m.find();
            rechargeBean.setMfdl(m.group(1));
            m.find();
            rechargeBean.setSfdl(m.group(1));
            m.find();
            rechargeBean.setLjdl(m.group(1));
            m.find();
            rechargeBean.setFdl(m.group(1));
            m.find();
            rechargeBean.setGdzt(m.group(1));
            rechargeBean.setSydl(String.valueOf(Float.valueOf(Float.valueOf(rechargeBean.getMfdl()).floatValue() + Float.valueOf(rechargeBean.getSfdl()).floatValue()-Float.valueOf(rechargeBean.getFdl()).floatValue())));
            rechargeBean.setMsg("0");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
        return rechargeBean;
    }

    @Override
    public RechargeBean showDlZTArray2(String xq,String fj,String xl) {
        RechargeBean rechargeBean =new RechargeBean();
        String result = "";
        Map params = new HashMap<String, String>();
        params.clear();
        try {
     /*       params.put("inMm", "60C85273A0D8834B798C5FA766D76B866B609AA755C3584F797F798C7FF8785D");
            params.put("inXq", xq);
            params.put("inFj", fj);
            params.put("cxLx:", xl);*/

            if(DkURL.length()<5){
                DkURL=iILwgyInoutElectronicService.getDkinterface();
            }
            System.out.println(">>>>>>>>>>>>>>>>>2" +DkURL);
            //result = WsdlUtil.callService(DkURL, "TraceDlZTArray",  params, result);
            result = wsCallService.callServiceHC(DkURL, xq,fj,xl );//山科大专用
            System.out.println(">>>>>>>>>>>>>>>>>" + result);


            String bb = String.valueOf(result);
            Pattern p = Pattern.compile("<string>(.*?)</string>");
            Matcher m = p.matcher(bb);
            m.find();
            if (m.group(1).equals("-1")||m.group(1).equals("ErrSb")||m.group(1).equals("-2")||m.group(1).equals("-3")) {
                rechargeBean.setMsg("1");
                return rechargeBean;
            }
/*
Y(ArrayfString xmlns:xsihttp://www w3, org/2001/XL Schema-instance’xmlnsxsd-"http://wwr, w. org/2001/L Schema"xnlns-"http://wkService. asmx/"

<string>正在供电</string>*/
            rechargeBean.setFjh(m.group(1));//<string>1303009</string>
            m.find();
            rechargeBean.setMfdl(m.group(1));//<string>0.00</string>
            m.find();
            rechargeBean.setSfdl(m.group(1));//<string>1.99</string>
            m.find();
            rechargeBean.setLjdl(m.group(1));//(string>895.69</string>
            m.find();
            rechargeBean.setFdl(m.group(1));//<string>0.00</string>
            m.find();
            rechargeBean.setGdzt(m.group(1));//<string>0</string>//华子哥说这个值没有意义
            rechargeBean.setGdzt(m.group(1));//<string>正在供电</string>
            rechargeBean.setSydl(String.valueOf(Float.valueOf(Float.valueOf(rechargeBean.getMfdl()).floatValue() + Float.valueOf(rechargeBean.getSfdl()).floatValue()-Float.valueOf(rechargeBean.getFdl()).floatValue())));
            rechargeBean.setMsg("0");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
        return rechargeBean;
    }

    @Override
    public String rechargeDLData(String xq, String fj, String dl, String id,String lsh,String lx) {
        String result = "";
        Map params = new HashMap<String, String>(16);
        params.clear();
        try {
            params.put("inMm", "7A0E6BC9978E804D77047953B54F7AA8863E71671E891E99");
            params.put("inXq", xq);
            params.put("inFj", fj);
            params.put("inDl", dl);
            params.put("inID", id);
            params.put("inLsh", lsh);
            params.put("inLx", lx);

            if(DkCdURL.length()<5){
                DkCdURL=iILwgyInoutElectronicService.getInterfaceAddress();
            }
            System.out.println(">>>>>>>>>>>>>>>>>2" +DkCdURL);
            result = wsCallService.callDkService(DkCdURL, xq,fj,dl,id,lsh,lx);
            System.out.println(">>>>>>>>>>>>>>>>>" + result);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return null;
        }
        return result;
    }
}
